// Problem:    177
// Title:      Integer angled Quadrilaterals
// Date:       11-Jan-2008
// Difficulty: 
// Fun Rating: 
// Time:       
// Space:      
// Closed:     
// Answer:     

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>

// Degree to radius
static double d2r(int angle)
{
	const double pi = atan(1.0)*4.0;
	return angle/180.0*pi;
}

// Radius to degree
static double r2d(double angle)
{
	const double pi = atan(1.0)*4.0;
	return angle*180.0/pi;
}

static int form_quadrilateral(int A, int B, int C, int D)
{
#define COMBINE(a,b,c,d) ((a<<24)|(b<<16)|(c<<8)|d)
	int p1 = std::min(COMBINE(A,B,C,D), COMBINE(B,C,D,A));
	int p2 = std::min(COMBINE(C,D,A,B), COMBINE(D,A,B,C));
	int p3 = std::min(COMBINE(D,C,B,A), COMBINE(C,B,A,D));
	int p4 = std::min(COMBINE(B,A,D,C), COMBINE(A,D,C,B));
	int p = std::min(std::min(p1,p2), std::min(p3,p4));
	return p;
}

void solve_problem_177()
{
	const int min_angle = 45, max_angle = 45;

	std::vector<int> Q;

#if 0
	for (int A1 = 1; A1 < 180; A1++)
	{
		//std::cout << "A1 = " << A1 << std::endl;
		for (int C1 = 1; C1 < 180-A1; C1++)
		{
			for (int A2 = 1; A2 < 180-A1; A2++)
#else
	for (int A1 = 45; A1 <= 45; A1++)
	{
		for (int C1 = 37; C1 <= 37; C1++)
		{
			for (int A2 = 40; A2 <=50; A2++)
#endif
			{
				for (int C2 = 1; C2 < 180-A2 && C2 < 180-C1; C2++)
				{
					int A = A1+A2;
					int B = 180-A1-C1;
					int D = 180-A2-C2;

					if (B==90 && D==90)
					{
						int k = 1;
					}

					double a = sin(d2r(C1)) / sin(d2r(B));
					double b = sin(d2r(C2)) / sin(d2r(D));
					double c = sqrt(a*a+b*b-2*a*b*cos(d2r(A)));
					double sin_alpha = b/c*sin(d2r(A));
					double alpha = r2d(asin(sin_alpha));
					if (fabs(alpha - (int)alpha) < 1e-9)
					{
						if (1)
						{
							std::cout << "Found: " 
								<< "A1=" << A1 << ", C1=" << C1 << ", A2=" << A2 << ", C2=" << C2
								<< ", Alpha=" << (int)alpha << std::endl;
						}
						Q.push_back(form_quadrilateral(A, B, C1+C2, D));
					}
				}
			}
		}
	}

	// Remove duplicate items in Q.
	std::sort(Q.begin(), Q.end());
	std::cout << "With dup = " << Q.size() << std::endl;
	std::cout << "No dup = " << (std::unique(Q.begin(), Q.end()) - Q.begin()) << std::endl;
}
